<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="油企id" prop="enterpriseId">
            <el-select
              v-model="queryParams.enterpriseId"
              placeholder="请选择油企id"
              clearable
              size="small"
              style="width: 190px"
            >
              <el-option label="请选择" value="" />
              <el-option
                :label="ele.enterpriseName"
                :value="ele.enterpriseId"
                v-for="ele in currentEnterprise"
                :key="ele.enterpriseId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="global_el_rowStyle">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          type="primary"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:dialogue:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:dialogue:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:dialogue:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:dialogue:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      stripe
      v-loading="loading"
      :data="dialogueList"
      @selection-change="handleSelectionChange"
    >
      <af-table-column type="selection" align="left" />
      <af-table-column
        label="id"
        align="left"
        width="100"
        prop="adId"
        sortable
      />
      <af-table-column
        label="油企id"
        align="left"
        width="150"
        prop="enterpriseId"
        sortable
      />
      <af-table-column
        label="名称"
        align="left"
        width="120"
        prop="adName"
        sortable
      />
      <af-table-column
        label="类型"
        align="left"
        width="120"
        prop="type"
        sortable
      />
      <af-table-column
        label="文本消息内容"
        align="left"
        width="180"
        prop="content"
        sortable
      />
      <af-table-column
        label="图片媒体ID"
        align="left"
        width="180"
        prop="mediaId"
        sortable
      />
      <af-table-column
        label="消息标题"
        align="left"
        width="120"
        prop="title"
        sortable
      />
      <af-table-column
        label="图文链接消息"
        align="left"
        width="180"
        prop="description"
        sortable
      />
      <af-table-column
        label="图文链接跳转链接"
        align="left"
        width="180"
        prop="url"
        sortable
      />
      <af-table-column
        label="图片链接图片地址"
        align="left"
        width="180"
        prop="thumbUrl"
        sortable
      />
      <af-table-column
        label="小程序页面路径"
        align="left"
        width="180"
        prop="pagepath"
        sortable
      />
      <af-table-column
        label="小程序卡片封面的media_id"
        align="left"
        prop="thumbMediaId"
        sortable
      />
      <af-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        sortable
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:dialogue:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:dialogue:remove']"
            >删除</el-button
          >
        </template>
      </af-table-column>
    </el-table>

    <div class="pagination_center">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改ad对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="油企id" prop="enterpriseId">
          <el-select v-model="form.enterpriseId" placeholder="请选择油企id">
            <el-option label="请选择" value="" />
            <el-option
              :label="ele.enterpriseName"
              :value="ele.enterpriseId"
              v-for="ele in currentEnterprise"
              :key="ele.enterpriseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Ad位名称" prop="adName">
          <el-input v-model="form.adName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="文本消息" value="text" />
            <el-option label="图片消息" value="image" />
            <el-option label="图文链接" value="link" />
            <el-option label="小程序卡片" value="miniprogrampage" />
          </el-select>
        </el-form-item>
        <el-form-item label="文本消息内容" v-if="form.type == 'text'">
          <!--editor v-model="form.content" :min-height="192"/>-->
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入文本内容"
          />
        </el-form-item>
        <el-form-item label="媒体ID" prop="mediaId" v-if="form.type == 'image'">
          <el-input v-model="form.mediaId" placeholder="请输入媒体ID" />
        </el-form-item>
        <el-form-item
          label="消息标题"
          prop="title"
          v-if="form.type == 'link' || form.type == 'miniprogrampage'"
        >
          <el-input v-model="form.title" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item
          label="图文链接消息"
          prop="description"
          v-if="form.type == 'link'"
        >
          <el-input v-model="form.description" placeholder="请输入图文链接" />
        </el-form-item>
        <el-form-item
          label="图文链接跳转链接"
          prop="url"
          v-if="form.type == 'link'"
        >
          <el-input v-model="form.url" placeholder="请输入图文链接跳转链接" />
        </el-form-item>
        <el-form-item
          label="图片链接图片地址"
          prop="thumbUrl"
          v-if="form.type == 'link'"
        >
          <el-input
            v-model="form.thumbUrl"
            placeholder="请输入图片链接图片地址"
          />
        </el-form-item>
        <el-form-item
          label="小程序页面路径"
          prop="pagepath"
          v-if="form.type == 'miniprogrampage'"
        >
          <el-input
            v-model="form.pagepath"
            placeholder="请输入小程序的页面路径，type=miniprogrampage必填"
          />
        </el-form-item>
        <el-form-item
          label="卡片封面的媒体id"
          prop="thumbMediaId"
          v-if="form.type == 'miniprogrampage'"
        >
          <el-input
            v-model="form.thumbMediaId"
            placeholder="请输入小程序消息卡片的封面"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDialogue,
  getDialogue,
  delDialogue,
  addDialogue,
  updateDialogue,
  exportDialogue,
} from "@/api/customer/dialogue";
import Editor from "@/components/Editor";

import { getEnterpriseList, getStationList } from "@/api/dept";

export default {
  name: "Dialogue",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ad表格数据
      dialogueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
      },
      currentEnterprise: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList();
  },
  methods: {
    getEnterpriseList() {
      getEnterpriseList({
        pageSize: 10000,
        pageNum: 1,
      }).then((res) => {
        this.currentEnterprise = res.rows;
      });
    },
    /** 查询ad列表 */
    getList() {
      this.loading = true;
      listDialogue(this.queryParams).then((response) => {
        this.dialogueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        adId: null,
        enterpriseId: null,
        adName: null,
        type: null,
        content: null,
        mediaId: null,
        title: null,
        description: null,
        url: null,
        thumbUrl: null,
        pagepath: null,
        thumbMediaId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.adId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Ad位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const adId = row.adId || this.ids;
      getDialogue(adId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Ad位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.adId != null) {
            updateDialogue(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDialogue(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const adIds = row.adId || this.ids;
      this.$confirm('是否确认删除ad编号为"' + adIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delDialogue(adIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有ad数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportDialogue(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
